<template>
  <div class="container">
    <el-card class="box-card">
      <el-form :inline="true" label-width="100px" :model="dirFrom">
        <el-form-item label="目录名称" prop="name">
          <el-input v-model="dirFrom.directoryName" size="small"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="name">
          <el-select v-model="dirFrom.state" placeholder="请选择" size="small">
            <el-option
              v-for="(item, index) in ['禁用', '启用']"
              :key="item"
              :label="item"
              :value="index"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-row type="flex" justify="center" style="width: 100%">
            <el-col>
              <el-button size="small" @click="clear">清除</el-button>
              <el-button size="small" type="primary" @click="search"
                >搜索</el-button
              >
            </el-col>
            <el-col :span="4">
              <el-button size="small" type="success" @click="addDirectorys"
                ><i class="el-icon-edit"></i> 新增目录</el-button
              ></el-col
            >
          </el-row>
        </el-form-item>
        <template>
          <el-alert
            :title="`数据一共` + this.content.counts + `条`"
            type="info"
            show-icon
            :closable="false"
          >
          </el-alert>
        </template>
      </el-form>
      <el-table :data="tableData" v-loading="loading" style="width: 100%">
        <el-table-column type="index" label="序号" width="80px">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科" width="180">
        </el-table-column>
        <el-table-column prop="directoryName" label="目录名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期"> </el-table-column>
        <el-table-column prop="totals" label="面试题数量"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template v-slot="{ row }">
            {{ row.state === 1 ? '已启用' : '已禁用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="text" size="mini" @click="disable(row)">{{
              row.state === 1 ? '启用' : '禁用'
            }}</el-button>
            <el-divider direction="vertical" />
            <el-button
              type="text"
              size="mini"
              :disabled="row.state === 0 ? true : false"
              @click="revise(row)"
              >修改</el-button
            >
            <el-divider direction="vertical" />
            <el-button
              type="text"
              size="mini"
              @click="delList(row)"
              :disabled="row.state === 0 ? true : false"
              >删除</el-button
            >
            <el-divider direction="vertical" />
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" justify="end">
        <el-pagination
          background
          :current-page="parseInt(page.page)"
          :page-sizes="[10, 20]"
          :page-size="parseInt(page.pagesize)"
          layout=" prev, pager, next, sizes,jumper"
          :total="page.counts"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </el-row>
      <!-- 弹框 -->
      <el-dialog
        :title="`${fromDir.id ? '修改学科' : '新增学科'}`"
        width="400px"
        :visible="showDialog"
        @close="closeDialog"
      >
        <el-form
          ref="formRef"
          :model="fromDir"
          :rules="rules"
          label-width="80px"
        >
          <el-form-item label="所属学科">
            <el-select
              size="small"
              v-model="value"
              style="width: 100%"
              placeholder="请选择"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="directoryName">
            <el-input size="small" v-model="fromDir.directoryName"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button>取消</el-button>
          <el-button type="primary" @click="addOk">确定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  list,
  remove,
  changeState,
  simple,
  add,
  update
} from '@/api/hmmm/directorys.js'
export default {
  name: 'Directorys',
  data () {
    return {
      tableData: [],
      showDialog: false,
      fromDir: {
        subjectName: [],
        directoryName: ''
      },
      loading: false,
      options: [],
      value: '',
      page: {
        page: null,
        pageSize: null
      },
      content: {
        counts: null
      },
      rules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      },
      dirFrom: {
        state: '',
        directoryName: ''
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    handleSizeChange (newPagesize) {
      this.page.pageSize = newPagesize
      this.getList()
    },
    handleCurrentChange (newPage) {
      this.page.page = newPage
      this.getList()
    },
    // 获取列表
    async getList () {
      const { data } = await list()
      // console.log(data)
      this.tableData = data.items
    },
    // 搜索
    search () {
      this.tableData = this.tableData.filter((v) => {
        return (
          v.directoryName === this.dirFrom.directoryName &&
          v.state === this.dirFrom.state
        )
      })
    },
    // 清除
    clear () {
      this.dirFrom.directoryName = ''
      this.dirFrom.state = ''
      this.getList()
    },
    // 删除
    async delList (row) {
      await this.$confirm('确定删除吗？', '提示', { type: 'warning' })
      await remove(row)
      this.getList()
    },
    async disable (row) {
      const res = await changeState({
        id: row.id,
        state: row.state ? 0 : 1
      })
      console.log(res)
      this.getList()
    },
    // 新增
    async addDirectorys (id) {
      this.showDialog = true
      const res = await simple(id)
      console.log(res)
      this.options = res.data
    },
    // 修改
    async revise () {
      this.showDialog = true
      const res = await simple()
      this.options = res.data
    },
    async addOk () {
      await this.$refs.formRef.validate()
      if (!this.fromDir.id) {
        await add({
          ...this.fromDir,
          subjectID: this.value
        })
        this.$message.success('添加成功！')
      } else {
        await update({
          id: this.fromDir.id,
          subjectID: this.fromDir.subjectID,
          directoryName: this.fromDir.directoryName
        })
        this.$message.success('修改成功！')
      }

      this.getList()
      this.closeDialog()
    },
    closeDialog () {
      this.showDialog = false
      this.fromDir = null
      this.fromDir = {}
      this.value = ''
    }
  }
}
</script>

<style scoped lang="less">
.box-card {
  margin: 10px 0 0 10px;
  .addbtn {
    margin-left: 450px;
  }
}
</style>
